/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.device.modular.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import vip.xiaonuo.device.core.enums.Constants;
import vip.xiaonuo.device.core.enums.DeviceErrEnum;
import vip.xiaonuo.device.modular.entity.ApiLog;
import vip.xiaonuo.device.modular.entity.BizCabinetDoor;
import vip.xiaonuo.device.modular.mapper.CabinetDoorMapper;
import vip.xiaonuo.device.modular.service.DevApiLogService;
import vip.xiaonuo.device.vo.Result;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 设备api日志Service接口实现类
 *
 * @author levin
 * @date 2022/11/30 13:27
 **/
@Service("deviceOnlineService")
public class DeviceOnlineServiceImpl extends DeviceBaseService {
    @Override
    protected Integer getReqType() {
        return Constants.CLIENTONLINE_P;
    }

    @Resource
    private CabinetDoorMapper cabinetDoorMapper;

    @Resource
    private DevApiLogService devApiLogService;

    @Override
    protected Result checkData(JSONObject param) {
        JSONArray cabCodeList = param.getJSONArray("data");
        if (cabCodeList == null) {
            return Result.fail("柜门code信息缺失");
        }
        if (cabCodeList.size() == 0) {
            return Result.fail("柜门code信息不存在");
        }
        return Result.ok();
    }

    @Override
    protected void handleWithParentLog(ApiLog log, JSONObject params) {
        JSONArray cabCodeList = params.getJSONArray("data");
        List<ApiLog> apiLogList = new ArrayList<>();
        for (Object item : cabCodeList) {
            String cabCode = item.toString();
            //根据柜子CODE获取柜门列表
            List<BizCabinetDoor> doorList = cabinetDoorMapper.getListByCabCode(cabCode);
            for (BizCabinetDoor door : doorList) {
                ApiLog apiLog = new ApiLog();
                apiLog.setParentId(log.getId());
                apiLog.setCabinetCode(cabCode);
                apiLog.setCabinetDoorCode(door.getCabinetDoorCode());
                apiLog.setReqJson(log.getReqJson());
                apiLog.setReqType(Constants.CLIENTONLINE);
                apiLog.setEventTime(log.getEventTime());
                apiLog.setActCompTime(door.getActTime());
                //更新柜门动作时间
                int count = cabinetDoorMapper.updateActTime(door.getId(),log.getEventTime());
                if(count==0){
                    log.setResulltType(DeviceErrEnum.REQ_ACT_TIMEOUT.getValue());
                }else{
                    apiLog.setResulltType(DeviceErrEnum.SUCCESS.getValue());
                }
                apiLogList.add(apiLog);
            }
        }
        if (!CollectionUtils.isEmpty(apiLogList)) {
            devApiLogService.saveBatch(apiLogList);
        }
    }
}
